<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Event extends AppModel {

    public $validate = array(
        'name' => array(
            'rule' => 'notEmpty'
        ),
        'description' => array(
            'rule' => 'notEmpty'
        )
    );

    function isUnique($field, $value) {
        $fields[$this->name . '.' . $field] = $value;


        $this->recursive = -1;
        if ($this->hasAny($fields)) {
            $this->invalidate('unique_' . $field);
            return false;
        }
        else
            return true;
    }

    /*
     * Description of Function getEvents
     * @accID
     * The function returns all the events belonging to the account with accountID = $accID
     * @author Mina Samy
     */

    public function getEvents($accID) {
        $count = $this->find('count', array("Event.accountID" => $accID));
        if ($count != 0) {
            $conditions = array("Event.accountID" => $accID, "Event.deleted" => false);
            return $this->find('all', array('conditions' => $conditions));
        } else {
            return array();
        }
    }

    public function testfindtweets() {
        $this->loadModel('FutureTweet');
        return $this->FutureTweet->findtweetstest();
    }

    /*
     * Description of Function setStatus
     * @status
     * @eventID
     * The function setStatus updates the statues of an event with id = $eventID according to the status bit given if status = 1 the event is completed 
     * if the status = 0 the event is not completed
     * @author Mina Samy
     */

    public function setStatus($eventID, $status) {
        if ($status == 1) {
            $this->updateAll(
                    array('status' => "'completed'"), array('id' => $eventID));
        } else {
            $this->updateAll(
                    array('status' => "'incomplete'"), array('id' => $eventID));
        }
    }

    /*
     * Description of Function isComplete
     * @eventID
     * The function returns true if the event with id = eventID is completed else it returns false
     * @author Mina Samy
     */

    public function isComplete($eventID) {
        $conditions = array("Event.id" => $eventID);
        $count = $this->find('count', array('conditions' => $conditions));
        if ($count == 1) {
            return true;
        } else {
            return false;
        }
    }

    /*
     * Description of Function isCreator
     * @userID
     * @eventID
     * The function returns true if there exists an event with id = eventID and creator = $userID
     * @author Mina Samy
     */

    public function isCreator($userID, $eventID) {
        $conditions = array("Event.creatorID" => $userID, "Event.id" => $eventID);
        $count = $this->find('count', array('conditions' => $conditions));
        if ($count == 1) {
            return true;
        } else {
            return false;
        }
    }

}

?>
